import { reactive, ref } from 'vue'
interface ITags {
  tag_id: string
  tag_name: string
}

export interface IFormState {
  video_id: string | undefined
  video_name: string | undefined
  role_name: string | undefined
  role_name_alias: string | undefined
  role_name_eng: string | undefined
  person_id: string | undefined
  person_name: string | undefined
  role_introduction: string | undefined
  role_cover: string | undefined
  role_cover_type: number
  role_status: number
  role_keyword: string | undefined
  role_tags: ITags[] | undefined
  role_tags_copy: ITags[] | undefined
  role_pop_compensated: number
  role_pop_add_star: number
  role_pop_add_total: number
  role_pop_add_day: number
  role_pop_add_week: number
  role_pop_add_month: number
  role_pop_add_view: number
  role_pop_add_like: number
  role_pop_add_dislike: number
  role_pop_star: number
  role_pop_total: number
  role_pop_day: number
  role_pop_week: number
  role_pop_month: number
  role_pop_view: number
  role_pop_like: number
  role_pop_dislike: number
  site_id?: string | undefined
  role_id?: string | undefined
}

// 状态管理
export const formState = reactive<IFormState>({
  video_id: undefined,
  video_name: undefined,
  role_name: undefined,
  role_name_alias: undefined,
  role_name_eng: undefined,
  person_id: undefined,
  person_name: undefined,
  role_introduction: undefined,
  role_cover: undefined,
  role_cover_type: 1,
  role_status: 1,
  role_keyword: undefined,
  role_tags: undefined,
  role_tags_copy: undefined,
  role_pop_compensated: 2,
  role_pop_add_star: 0,
  role_pop_add_total: 0,
  role_pop_add_day: 0,
  role_pop_add_week: 0,
  role_pop_add_month: 0,
  role_pop_add_view: 0,
  role_pop_add_like: 0,
  role_pop_add_dislike: 0,
  role_pop_star: 0,
  role_pop_total: 0,
  role_pop_day: 0,
  role_pop_week: 0,
  role_pop_month: 0,
  role_pop_view: 0,
  role_pop_like: 0,
  role_pop_dislike: 0,
  site_id: undefined,
  role_id: undefined
})
export interface ClearExtendInfo {
  role_pop_add_total: number
  role_pop_add_day: number
  role_pop_add_week: number
  role_pop_add_month: number
  role_pop_add_view: number
  role_pop_add_like: number
  role_pop_add_dislike: number
  role_pop_add_star: number
}
//随机生成区间范围数
const selectFrom = (lowerValue: number, upperValue: number) => {
  let choices = upperValue - lowerValue + 1
  return Math.floor(Math.random() * choices + lowerValue)
}
//判断是否是小数
const isDot = (num: number) => {
  if (String(num).indexOf('.') > -1) {
    return true
  } else {
    return false
  }
}
//以0.5为一个step向上取整
const floorFunc = (num: number) => {
  if (isDot(num)) {
    const int = parseInt(num.toString())
    const floatNum = num - int
    if (floatNum < 0.5) {
      return int + 0.5
    } else {
      return int + 1
    }
  }
  return num
}
//重置扩展信息
export const resetExtendFunc = () => {
  const form = {
    role_pop_add_total: formState.role_pop_add_total,
    role_pop_add_day: formState.role_pop_add_day,
    role_pop_add_week: formState.role_pop_add_week,
    role_pop_add_month: formState.role_pop_add_month,
    role_pop_add_view: formState.role_pop_add_view,
    role_pop_add_like: formState.role_pop_add_like,
    role_pop_add_dislike: formState.role_pop_add_like,
    role_pop_add_star: formState.role_pop_add_star
  }
  let key: keyof ClearExtendInfo
  for (key in form) {
    formState[key] = 0
  }
}
//随机补偿方法
export const randomFunc = () => {
  const data = {
    role_pop_add_total: 0,
    role_pop_add_day: 0,
    role_pop_add_week: 0,
    role_pop_add_month: 0,
    role_pop_add_view: 0,
    role_pop_add_like: 0,
    role_pop_add_dislike: 0,
    role_pop_add_star: 0
  }
  data.role_pop_add_view = selectFrom(1000, 5000) //总浏览
  const monthView = +(data.role_pop_add_view / 2).toFixed(0) //月浏览
  const weekView = +(data.role_pop_add_view / 8).toFixed(0) //周浏览
  const dayView = +(data.role_pop_add_view / 60).toFixed(0) //日浏览
  data.role_pop_add_like = selectFrom(10, 600) //总点赞
  const monthLike = +(data.role_pop_add_like / 2).toFixed(0) //月点赞
  const weekLike = +(data.role_pop_add_like / 8).toFixed(0) //周点赞
  const dayLike = +(data.role_pop_add_like / 60).toFixed(0) //日点赞
  data.role_pop_add_dislike = 0 //总点踩
  const monthDislike = 0 //月点踩
  const weekDislike = 0 //周点踩
  const dayDislike = 0 //日点踩
  //总人气
  data.role_pop_add_total = data.role_pop_add_view + 100 * data.role_pop_add_like + 100 * data.role_pop_add_dislike
  //月人气
  data.role_pop_add_month = monthView + 100 * monthLike + 100 * monthDislike
  //周人气
  data.role_pop_add_week = weekView + 100 * weekLike + 100 * weekDislike
  //日人气
  data.role_pop_add_day = dayView + 100 * dayLike + 100 * dayDislike
  //随机评分，用于计算星级
  const score = selectFrom(6.0, 10.0)
  //星级
  data.role_pop_add_star = floorFunc(score / 2)
  console.log('score: ', score.toFixed(1))
  console.log('role_pop_add_star: ', data.role_pop_add_star.toFixed(1))
  Object.assign(formState, data)
  console.log('formState: ', formState)
}
//开启关闭补偿按钮状态
export const isPopularity = ref(false)
